﻿$(document).ready(function () {

    // MODELS
    function statusModel(item) {
        this.Id = ko.observable(item.Id),
        this.Status = ko.observable(item.Status)
    };

    function categoryModel(item) {
        this.Id = ko.observable(item.Id),
        this.Name = ko.observable(item.Name)
    };

    function personModel(item) {
        this.UserID = ko.observable(item.UserID),
        this.DisplayName = ko.observable(item.DisplayName)
    };

    // VIEWMODEL
    var viewModel = {

        statuses: ko.observableArray([]),
        categories: ko.observableArray([]),
        people: ko.observableArray([]),

        Id: ko.observable(),
        ModuleId: ko.observable(0),
        Person: ko.observable(""),
        Task: ko.observable(),
        selectedStatus: ko.observable(),
        selectedPerson: ko.observable(),
        selectedCategory: ko.observable(),

        loadStatuses: function () {
            OData.read(_serviceURL + "/ToDoListStatus", function (response) {
                viewModel.statuses.removeAll();
                $.each(response.results, function (index, item) {
                    viewModel.statuses.push(new statusModel(item));
                });
            });
        },

        loadCategories: function () {
            OData.read(_serviceURL + "/ToDoListCategories?$orderby=Name", function (response) {
                viewModel.categories.removeAll();
                $.each(response.results, function (index, item) {
                    viewModel.categories.push(new categoryModel(item));
                });
            });
        },

        loadPeople: function () {
            OData.read(_serviceURL + "/Users", function (response) {
                viewModel.people.removeAll();
                $.each(response.results, function (index, item) {
                    viewModel.people.push(new personModel(item));
                });
            });
        },

        loadItem: function () {
            OData.read(_serviceURL + "/ToDoListItems(" + _id + ")?$expand=Status,Category,User", function (response) {
                viewModel.Id(response.Id);
                viewModel.Task(response.Task);
                viewModel.Person(response.User.DisplayName);
                viewModel.selectedCategory(response.Category.Id);
                viewModel.selectedPerson(response.User.UserID);
                viewModel.selectedStatus(response.Status.Id);
            });
        },

        updateTask: function () {

            var putdata = {
                Id: viewModel.Id(),
                ModuleId: viewModel.ModuleId(),
                CreatedDate: new Date(),
                Task: viewModel.Task(),
                User: { __metadata: { uri: "/Users(" + viewModel.selectedPerson() + ")"} },
                Status: { __metadata: { uri: "/ToDoListStatus(" + viewModel.selectedStatus() + ")" } },
                Category: { __metadata: { uri: "/ToDoListCategories(" + viewModel.selectedCategory() + ")" } }
            };

            var _request = {
                requestUri: _serviceURL + "/ToDoListItems(" + viewModel.Id() + ")",
                method: "MERGE",
                data: putdata
            };

            // POST new game to the service
            OData.request(_request, function (data) {
                document.location.href = _returnURL;
            });
        },

        deleteTask: function () {
            var putdata = {
                Id: viewModel.Id()
            };

            var _request = {
                requestUri: _serviceURL + "/ToDoListItems(" + viewModel.Id() + ")",
                method: "DELETE",
                data: putdata
            };

            var _confirm = confirm("Delete '" + viewModel.Task() + "' ?")
            if (_confirm) {
                OData.request(_request, function (data) {
                    document.location.href = _returnURL;
                });
            }
        }

    };

    // Activate knockout.js
    ko.applyBindings(viewModel);

    viewModel.loadStatuses();
    viewModel.loadCategories();
    viewModel.loadPeople();

    viewModel.loadItem();

});